package com.mapler.job;

import com.mapler.service.INotifier;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author mithun
 */
public class CLAutoREngine implements ICLAutoREngine {    
    protected ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(7);
    protected ScheduledFuture<?> emailPullerHandler = null;
    protected ScheduledFuture<?> emailSenderHandler = null;
    INotifier iNotifier;

    @Override
    public void startEmailPuller() {
        try {
            if (emailPullerHandler != null && !emailPullerHandler.isCancelled() && !scheduler.isShutdown()) {
                return;
            }
            CLAutoREmailPuller clAutoREmailPuller = new CLAutoREmailPuller();
            emailPullerHandler = scheduler.scheduleAtFixedRate(clAutoREmailPuller, 1, 2, TimeUnit.MINUTES);
            clAutoREmailPuller.run();
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void stopEmailPuller() {
        emailPullerHandler.cancel(true);
        scheduler.shutdown();
        emailPullerHandler = null;
        scheduler = Executors.newScheduledThreadPool(7);
    }

    @Override
    public void startEmailSender() {
        try {
            if (emailPullerHandler != null && !emailPullerHandler.isCancelled() && !scheduler.isShutdown()) {
                return;
            }
            CLAutoREmailSender clAutoREmailSender = new CLAutoREmailSender();
            emailSenderHandler = scheduler.scheduleAtFixedRate(clAutoREmailSender, 1, 2, TimeUnit.MINUTES);
            clAutoREmailSender.run();
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void stopEmailSender() {
        emailSenderHandler.cancel(true);
        scheduler.shutdown();
        emailSenderHandler = null;
        scheduler = Executors.newScheduledThreadPool(7);
    }
}
